package com.nondo.dean.product.controller.api.app;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.common.CategoryUtils;
import com.nondo.dean.product.domain.ProductWithBLOBs;
import com.nondo.dean.product.model.result.CategoryResult;
import com.nondo.dean.product.model.result.ProductBeanResult;
import com.nondo.dean.product.service.ProductCategoryService;
import com.nondo.dean.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xiaofan
 * @Description:
 * @throws
 * @date 2018-04-0817:03
 * @Version 0.1
 */
@RestController("apiAppProductCategory")
@RequestMapping("/{v}/app/category")
public class ProductCategoryController {

    @Autowired
    ProductCategoryService productCategoryService;

    @Autowired
    ProductService productService;


    /**
     * 获取产品所有分类列表不带分页
     * @param request
     * @param response
     * @param
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<List<CategoryResult>> categoryResultList(HttpServletRequest request, HttpServletResponse response) {
        View<List<CategoryResult>> view = new View<List<CategoryResult>>(20000, "获取列表成功");
        List<CategoryResult> categoryResultList= productCategoryService.getCategoryAll();
        view.setData(categoryResultList);


        return view;
    }

    /**
     * 分类下面系列的产品数据展示
     * @param request
     * @param response
     * @param
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/product/list")
    public View<Page<ProductBeanResult>> categoryProducttList(
            HttpServletRequest request, HttpServletResponse response, @RequestBody Query<ProductWithBLOBs> query) {
        View<Page<ProductBeanResult>> view = new  View<Page<ProductBeanResult>>(20000, "获取列表成功");
        ProductWithBLOBs productWithBLOBs = query.getData();

        Page<ProductBeanResult> page = new Page<ProductBeanResult>(query.getPage(), query.getLimit(), this.productService.countProduct(productWithBLOBs));

        List<ProductBeanResult> list= productCategoryService.productClassification(query.getOffset(),query.getLimit(),productWithBLOBs.getCategoryId(),productWithBLOBs.getSeriesId());
        page.setContent(list);
        view.setData(page);
        return view;
    }
}
